/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MessageBoxDialog.java
 *
 * Created on 13.05.2010, 11:38:24
 */

package shaman.dialog;

import shaman.dialog.MessageBox.DialogResult;

/**
 *
 * @author Shaman
 */
public class MessageBoxDialog extends javax.swing.JDialog {

    private static final String ICON_PATH = "/shaman/dialog/resources/";
    private static final String ICON_ERROR = ICON_PATH + "dialog-error.png";
    private static final String ICON_WARNING = ICON_PATH + "dialog-warning.png";
    private static final String ICON_INFORMATION = ICON_PATH + "dialog-information.png";
    private static final String ICON_QUESTION = ICON_PATH + "dialog-question.png";

    /** Creates new form MessageBoxDialog */
    public MessageBoxDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void setIcon(MessageBox.DialogIcon icon)
    {
        switch (icon)
        {
            case ERROR: messageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(ICON_ERROR)));
                break;
            case INFORMATION: messageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(ICON_INFORMATION)));
                break;
            case WARNING: messageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(ICON_WARNING)));
                break;
            case QUESTION: messageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(ICON_QUESTION)));
                break;
            default: messageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/shaman/dialog/resources/dialog-information.svg")));
        }
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public DialogResult getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        noButton = new javax.swing.JButton();
        abortButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        retryButton = new javax.swing.JButton();
        yesButton = new javax.swing.JButton();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMinimumSize(messageLabel.getSize());
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        noButton.setText("No");
        noButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noButtonAction(evt);
            }
        });

        abortButton.setText("Abort");
        abortButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abortButtonAction(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonAction(evt);
            }
        });

        messageLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        messageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/shaman/dialog/resources/dialog-information.png"))); // NOI18N
        messageLabel.setText("jLabel1");

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonAction(evt);
            }
        });

        retryButton.setText("Retry");
        retryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                retryButtonAction(evt);
            }
        });

        yesButton.setText("Yes");
        yesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yesButtonAction(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(messageLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 467, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(yesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(noButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(retryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(abortButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(messageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(abortButton)
                    .addComponent(retryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(noButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(yesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonAction
        doClose(DialogResult.OK);
    }//GEN-LAST:event_okButtonAction

    private void cancelButtonAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonAction
        doClose(DialogResult.CANCEL);
    }//GEN-LAST:event_cancelButtonAction

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(DialogResult.CANCEL);
    }//GEN-LAST:event_closeDialog

    private void noButtonAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noButtonAction
        doClose(DialogResult.NO);
    }//GEN-LAST:event_noButtonAction

    private void retryButtonAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_retryButtonAction
        doClose(DialogResult.RETRY);
    }//GEN-LAST:event_retryButtonAction

    private void abortButtonAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abortButtonAction
        doClose(DialogResult.ABORT);
    }//GEN-LAST:event_abortButtonAction

    private void yesButtonAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yesButtonAction
        doClose(DialogResult.YES);
    }//GEN-LAST:event_yesButtonAction

    public void doClose(DialogResult retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() MessageBoxDialoglic void run()MessageBoxDialog  NewOkCancelDialog dialog = new NewOkCancelDialog(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton abortButton;
    public javax.swing.JButton cancelButton;
    private javax.swing.JLabel messageLabel;
    public javax.swing.JButton noButton;
    public javax.swing.JButton okButton;
    public javax.swing.JButton retryButton;
    public javax.swing.JButton yesButton;
    // End of variables declaration//GEN-END:variables

    private DialogResult returnStatus = DialogResult.CANCEL;

    void setMessage(String Message) {
        messageLabel.setText("<html>"+Message+"</html>");
        this.setSize(messageLabel.getWidth()+20, this.getHeight());
        this.update(this.getGraphics());
    }
}
